<?php

class SystemTaskLog extends AppModel
{
	public $name = 'SystemTaskLog';
	public $useTable = 'SYSTEM_TASK_LOG';
	public $primaryKey = 'ID';

	public $belongsTo = array(
	   'SystemTask' => array('className' => 'SystemTask', 'foreignKey' => 'SYSTEM_TASK_ID'),
	);

	function getRecords($from, $to)
	{
        $from = date('Y-m-d H:i:s', strtotime($from));
        $to   = date('Y-m-d H:i:s', strtotime($to));
	    $this->SystemTask->recursive = -1;
	    $tmp_tasks = $this->SystemTask->findAll();
	    $tasks = array();
	    foreach ($tmp_tasks as $task) {
	        $tasks[$task['SystemTask']['ID']] = $task;
	    }
	    $this->recursive = -1;
	    $tmp_records = $this->findAll(array(
	       "START_DATE <='{$to}'",
	       "FINISH_DATE >='{$from}'"));
	    $records = array();
	    foreach ($tmp_records as $record) {
            $record['SystemTaskLog']['SystemTask'] = $tasks[$record['SystemTaskLog']['SYSTEM_TASK_ID']]['SystemTask'];
            $records[$record['SystemTaskLog']['SYSTEM_TASK_ID']][$record['SystemTaskLog']['THREAD']][] = $record['SystemTaskLog'];
	    }

	    return $records;
	}
}

?>